package com.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.*;

/**
 * @Author: hdu_huang
 * @Date: 2019/1/15 14:22
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Order {

    private Integer id; //主键
    private String date; //日期 LocalDate
    private int userId; //用户id
    private int collegeId; //学校id [1,2,3,4]
    private int sex;  //性别 0 男 1女
    private int itemId; // 商品 id [1,2,3]


//    volatile static boolean flag = false;
    //-Xlog:jit+compilation=debug:file=jit_compile%t.log:uptime,level,tags:filecount=10,filesize=100M
    static int algorithm(int n) {
        int bestSoFar = 0;
        for (int i=0; i<n; ++i) {
            if (Thread.interrupted()) {
                System.out.println("broken by interrupted");
                break;
            }
            //增加pow计算，增加计算量，防止循环执行不超过1s就结束了
            bestSoFar = (int) Math.pow(i, 0.3);
        }
        return bestSoFar;
    }
    public static void main(String[] args) throws InterruptedException {
        Runnable task = () -> {
            Instant start = Instant.now();
            int bestSoFar = algorithm(1000000000);
            double durationInMillis = Duration.between(start, Instant.now()).toMillis();
            System.out.println("after "+durationInMillis+" ms, the result is "+bestSoFar);
        };

        //延迟1ms之后interrupt
        Thread t = new Thread(task);
        t.start();
        Thread.sleep(1);
        t.interrupt();

        //延迟10ms之后interrupt
        t = new Thread(task);
        t.start();
        Thread.sleep(10);
        t.interrupt();

        //延迟100ms之后interrupt
        t = new Thread(task);
        t.start();
        Thread.sleep(100);
        t.interrupt();

        //延迟1s之后interrupt
        //这时候 algorithm 里面的for循环调用次数应该足够了，会发生代码即时编译优化并 OSR
        t = new Thread(task);
        t.start();
        Thread.sleep(1000);
        //发现线程这次不会对 interrupt 有反应了
        t.interrupt();
    }

}
